<template>
  <div class="product-detail" v-if="product">
    <div class="product-container">
      <div class="product-gallery">
        <img :src="product.image" :alt="product.name" class="main-image">
      </div>
      
      <div class="product-info">
        <h1 class="product-title" v-text="product.name"></h1>
        <p class="product-category">分类: <span v-text="product.category"></span></p>
        
        <div class="product-rating">
          <div class="stars">
            <span v-for="star in 5" :key="star" class="star" v-text="star <= product.rating ? '★' : '☆'"></span>
            <span class="rating-text"><span v-text="product.rating"></span> (<span v-text="product.reviews"></span> 条评价)</span>
          </div>
        </div>
        
        <p class="product-price">¥<span v-text="product.price"></span></p>
        
        <div class="product-description">
          <h3>商品描述</h3>
          <p>这是一款高质量的 <span v-text="product.name"></span>，具有卓越的性能和设计。适合日常使用，提供优质的用户体验。</p>
        </div>
        
        <div class="product-actions">
          <div class="quantity-selector">
            <label>数量:</label>
            <button @click="decreaseQuantity" :disabled="quantity <= 1">-</button>
            <span class="quantity" v-text="quantity"></span>
            <button @click="increaseQuantity">+</button>
          </div>
          
          <button @click="addToCart" class="add-to-cart-btn">加入购物车</button>
          <button @click="buyNow" class="buy-now-btn">立即购买</button>
        </div>
      </div>
    </div>
    
    <div class="product-reviews">
      <h2>用户评价</h2>
      <div class="review-list">
        <div v-for="review in productReviews" :key="review.id" class="review-item">
          <div class="review-header">
            <span class="reviewer" v-text="review.user"></span>
            <div class="review-stars">
            <span v-for="star in 5" :key="star" class="star" v-text="star <= review.rating ? '★' : '☆'"></span>
          </div>
          </div>
          <p class="review-date" v-text="review.date"></p>
          <p class="review-content" v-text="review.content"></p>
        </div>
      </div>
    </div>
  </div>
  
  <div v-else class="product-not-found">
    <h2>商品未找到</h2>
    <p>抱歉，您查找的商品不存在或已被移除。</p>
    <router-link to="/products" class="back-link">返回商品列表</router-link>
  </div>
</template>

<script>
import { ref, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { mockProducts } from '../utils/mockData.js'

export default {
  name: 'ProductDetail',
  setup() {
    const route = useRoute()
    const router = useRouter()
    
    const productId = route.params.id
    const quantity = ref(1)
    
    // 使用模拟商品数据
    const product = computed(() => {
      return mockProducts.find(p => p.id == productId) || null
    })
    
    // 模拟评价数据
    const productReviews = ref([
      {
        id: 1,
        user: '张三',
        rating: 5,
        date: '2025-08-15',
        content: '商品质量非常好，完全符合描述，物流也很快，非常满意！'
      },
      {
        id: 2,
        user: '李四',
        rating: 4,
        date: '2025-08-10',
        content: '性价比很高，使用了一段时间，效果不错，推荐购买。'
      },
      {
        id: 3,
        user: '王五',
        rating: 5,
        date: '2025-08-05',
        content: '超出预期的好，外观时尚，功能强大，值得购买。'
      }
    ])
    
    const increaseQuantity = () => {
      quantity.value++
    }
    
    const decreaseQuantity = () => {
      if (quantity.value > 1) {
        quantity.value--
      }
    }
    
    const addToCart = () => {
      alert(`${product.value.name} 已加入购物车，数量: ${quantity.value}`)
    }
    
    const buyNow = () => {
      router.push('/checkout')
    }
    
    return {
      product,
      quantity,
      productReviews,
      increaseQuantity,
      decreaseQuantity,
      addToCart,
      buyNow
    }
  }
}
</script>

<style scoped>
.product-detail {
  padding: 2rem 0;
}

.product-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2rem;
  margin-bottom: 3rem;
}

.product-gallery {
  display: flex;
  justify-content: center;
  align-items: center;
}

.main-image {
  max-width: 100%;
  max-height: 500px;
  object-fit: contain;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.product-info {
  padding: 1rem;
}

.product-title {
  font-size: 1.8rem;
  margin-bottom: 0.5rem;
  color: #333;
}

.product-category {
  color: #666;
  margin-bottom: 1rem;
}

.product-rating {
  margin-bottom: 1rem;
}

.stars {
  display: flex;
  align-items: center;
}

.star {
  color: #ffc107;
  font-size: 1.2rem;
  margin-right: 2px;
}

.rating-text {
  margin-left: 0.5rem;
  color: #666;
}

.product-price {
  font-size: 1.8rem;
  font-weight: bold;
  color: #e91e63;
  margin-bottom: 1.5rem;
}

.product-description {
  margin-bottom: 2rem;
}

.product-description h3 {
  margin-bottom: 0.5rem;
  color: #333;
}

.product-description p {
  color: #666;
  line-height: 1.6;
}

.product-actions {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.quantity-selector {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.quantity-selector label {
  font-weight: bold;
}

.quantity-selector button {
  width: 30px;
  height: 30px;
  border: 1px solid #ddd;
  background: white;
  border-radius: 4px;
  cursor: pointer;
}

.quantity-selector button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.quantity {
  font-weight: bold;
  min-width: 30px;
  text-align: center;
}

.add-to-cart-btn, .buy-now-btn {
  padding: 1rem;
  font-size: 1.1rem;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.add-to-cart-btn {
  background-color: #42b983;
  color: white;
}

.add-to-cart-btn:hover {
  background-color: #359c6d;
}

.buy-now-btn {
  background-color: #e91e63;
  color: white;
}

.buy-now-btn:hover {
  background-color: #c2185b;
}

.product-reviews h2 {
  margin-bottom: 1.5rem;
  color: #333;
}

.review-list {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.review-item {
  background: white;
  padding: 1.5rem;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.review-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 0.5rem;
}

.reviewer {
  font-weight: bold;
  color: #333;
}

.review-stars {
  color: #ffc107;
}

.review-date {
  color: #666;
  font-size: 0.9rem;
  margin-bottom: 1rem;
}

.review-content {
  color: #333;
  line-height: 1.6;
}

.product-not-found {
  text-align: center;
  padding: 3rem;
}

.product-not-found h2 {
  margin-bottom: 1rem;
  color: #333;
}

.back-link {
  color: #42b983;
  text-decoration: none;
  font-weight: bold;
}

.back-link:hover {
  text-decoration: underline;
}

@media (max-width: 768px) {
  .product-container {
    grid-template-columns: 1fr;
  }
  
  .product-title {
    font-size: 1.5rem;
  }
  
  .product-price {
    font-size: 1.5rem;
  }
}
</style>